package com.dnjn.happiness.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.AdviceAssign;
import com.dnjn.happiness.biz.service.IAdviceAssignService;
import com.dnjn.happiness.biz.service.IAdviceService;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 建议分配表Controller
 *
 * @author happiness
 * @date 2023-10-16
 */
@Tag(name = "biz assign 建议分配表", description = "建议分配表")
@RestController
@RequestMapping("/assign")
public class AdviceAssignController extends BaseController {
    @Autowired
    private IAdviceAssignService adviceAssignService;

    @Autowired
    private IAdviceService adviceService;

    /**
     * 查询建议分配表列表
     */
    @RequiresPermissions("biz:assign:list")
    @PostMapping("/list")
    @Log(title = "biz", businessType = BusinessType.LIST)
    public AjaxResult list(@RequestBody AdviceAssign adviceAssign, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<AdviceAssign> list = adviceAssignService.page(page, Wrappers.query(adviceAssign));
        return success(list);
    }

    /**
     * 导出建议分配表列表
     */
    @Log(title = "建议分配表", businessType = BusinessType.EXPORT)
    @RequiresPermissions("biz:assign:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdviceAssign adviceAssign) {
        List<AdviceAssign> list = adviceAssignService.list(Wrappers.query(adviceAssign));
        ExcelUtil<AdviceAssign> util = new ExcelUtil<AdviceAssign>(AdviceAssign.class);
        util.exportExcel(response, list, "建议分配表数据");
    }

    /**
     * 获取建议分配表详细信息
     */
    @RequiresPermissions("biz:assign:query")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        return success(adviceAssignService.getById(id));
    }

    /**
     * 根据建议建议id 查看指派详情列表前端作用的这种格式
     */
    @GetMapping("/getAssignByAdviceId")
//    @GetMapping("/{adviceId}")  @PathVariable("adviceId") Long adviceId
    public AjaxResult getAssignByAdviceId(@RequestParam Long adviceId) {
        List<AdviceAssign> adviceAssignList = adviceAssignService.getAssignByAdviceId(adviceId);
        return success(adviceAssignList);
    }


    /**
     * 修改建议分配表
     */
    @Log(title = "建议分配表", businessType = BusinessType.UPDATE)
    @RequiresPermissions("biz:assign:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody AdviceAssign adviceAssign) {
        return toAjax(adviceAssignService.updateById(adviceAssign));
    }

    /**
     * 删除建议分配表
     */
    @Log(title = "建议分配表", businessType = BusinessType.DELETE)
    @RequiresPermissions("biz:assign:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(adviceAssignService.removeBatchByIds(Arrays.asList(ids)));
    }
}
